
public class DoubleNodeTest {

	public static void main(String[] args) {
		DoubleNode head = createNode(5);
		showNode(head);
		DoubleNode node = move(head, 5);
		showNode(node);
	}

	private static void showNode(DoubleNode head) {
		DoubleNode tmp = head;
		while (tmp != null) {
			System.out.print(tmp.value + "->");
			tmp = tmp.next;
		}
		System.out.println();
	}
	private static DoubleNode createNode(int num) {
		DoubleNode head = new DoubleNode(1);
		DoubleNode pre = head;
		for (int i = 2; i <= num; i++) {
			DoubleNode node = new DoubleNode(i);
			pre.next = node;
			node.pre = pre;
			pre = node;
		}
		return head;
	}

	private static DoubleNode move(DoubleNode node, int i) {
		if(node.value == i) {
			return node;
		}
		DoubleNode tmp = node;
		while (tmp != null) {
			if(tmp.value == i) {
				break;
			}
			tmp = tmp.next;
		}
		if(tmp != null) {
			DoubleNode pre = tmp.pre;
			DoubleNode next = tmp.next;
			pre.next = next;
			if(next != null) {
				next.pre = pre;
			}
			tmp.next = node;
			tmp.pre = null;
			node.pre = tmp;
			return tmp;
		}
		return node;
	}
}
